﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class frmSuaChiTieu : Form
    {
        public frmSuaChiTieu()
        {
            InitializeComponent();
        }
        public static string maso;
        connect cn = new connect();
        private void dsChiTietChiTieu(string machi)
        {

            SqlCommand cmd = new SqlCommand("tblChiTietChiTieu_DS", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("machi", SqlDbType.Int)).Value = machi;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gridChiTiet.DataSource = dt;
        }
        private void frmSuaChiTieu_Load(object sender, EventArgs e)
        {
            lbMaChi.Text = bientinhChiTieu.Machi;
            dsChiTietChiTieu(lbMaChi.Text);
            cobNguoiChi.DataSource = cn.getDataTable("tblNhanVien_DS");
            cobNguoiChi.DisplayMember = "HoTen";
            cobNguoiChi.ValueMember = "MaNV";
            cobNguoiChi.Text = bientinhChiTieu.Hoten;

            cobTenChi.DataSource = cn.getDataTable("tblDungCu_NguyenLieu_DS");
            cobTenChi.DisplayMember = "TenDungCu";
            cobTenChi.ValueMember = "MaDungCu";

            dtNgayChi.Value = bientinhChiTieu.Ngaychi;
            int i;
            double tong = 0;
            for (i = 0; i < gridChiTiet.Rows.Count; i++)
            {
                tong = tong + double.Parse(gridChiTiet.Rows[i].Cells["ThanhTien"].Value.ToString());
            }
            lbtongtien.Text = String.Format("{0:N0}", tong);
        }

        private void gridChiTiet_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                gridChiTiet.CurrentRow.Selected = true;
                cobTenChi.SelectedValue = gridChiTiet.CurrentRow.Cells["MaDungCu"].Value.ToString();
                txtSoLuong.Text = gridChiTiet.CurrentRow.Cells["SoLuong"].Value.ToString();
                txtDonGia.Text = gridChiTiet.CurrentRow.Cells["DonGia"].Value.ToString();
                //gridChiTiet.CurrentRow.Selected = true;
            }
            catch
            {
                
            }
        }
        private void thanhtien(string dongia, string soluong)
        {            
            if (txtDonGia.Text == "" || txtSoLuong.Text == "")
            {
                txtThanhTien.Text = "0";
            }
            else
            {
                txtThanhTien.Text = String.Format("{0:N0}", (double.Parse(dongia) * double.Parse(soluong)));
            }

        }
        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            if (txtSoLuong.Text != "")
            {
                txtSoLuong.Text = String.Format("{0:N0}", double.Parse(txtSoLuong.Text));
                txtSoLuong.SelectionStart = txtSoLuong.Text.Length;
                thanhtien(txtDonGia.Text, txtSoLuong.Text);
            }
            else
            {
                thanhtien(txtDonGia.Text, "0");
            }
        }

        private void txtDonGia_TextChanged(object sender, EventArgs e)
        {
            if (txtDonGia.Text != "")
            {
                txtDonGia.Text = String.Format("{0:N0}", double.Parse(txtDonGia.Text));
                txtDonGia.SelectionStart = txtDonGia.Text.Length;
                thanhtien(txtDonGia.Text, txtSoLuong.Text);
            }
            else
            {
                thanhtien("0", txtSoLuong.Text);
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnLuuChiTiet_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("tblChiTietChiTieu_Sua",cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("dungcu",SqlDbType.Int)).Value = cobTenChi.SelectedValue;
            cmd.Parameters.Add(new SqlParameter("machi",SqlDbType.Int)).Value = int.Parse(lbMaChi.Text);
            cmd.Parameters.Add(new SqlParameter("machitiet",SqlDbType.Int)).Value = int.Parse(gridChiTiet.CurrentRow.Cells["MaChiTiet"].Value.ToString());
            cmd.Parameters.Add(new SqlParameter("soluong",SqlDbType.Int)).Value = double.Parse(txtSoLuong.Text);
            cmd.Parameters.Add(new SqlParameter("dongia",SqlDbType.Int)).Value = double.Parse(txtDonGia.Text);
            if(cmd.ExecuteNonQuery()==1)
            {
                dsChiTietChiTieu(lbMaChi.Text);
            }

        }
        private bool kiemtrahoten()
        {
            SqlCommand cmd = new SqlCommand("tblNhanVien_KiemTraTenNV", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("tennv", SqlDbType.NVarChar)).Value = cobNguoiChi.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count <= 0)
            {
                return false;
            }
            return true;
        }
        private bool kiemtradungcu()
        {
            SqlCommand cmd = new SqlCommand("tblDungCuNguyenLieu_KiemTraTen", cn.ketnoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("ten", SqlDbType.NVarChar)).Value = cobTenChi.Text;
            object ob = cmd.ExecuteScalar();
            if (Convert.ToInt32(ob) <= 0)
            {
                return false;
            }
            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (kiemtrahoten() == false)
            {
                MessageBox.Show("Nhân viên " + cobNguoiChi.Text + " chưa có trong danh sách nhân viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cobNguoiChi.Focus();
            }
            else if (kiemtradungcu() == false)
            {
                MessageBox.Show("Dụng cụ không có trong hệ thống!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cobTenChi.Focus();
            }
            else
            {
                SqlCommand cmd = new SqlCommand("tblChiTieu_Sua", cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("machi", SqlDbType.Int)).Value = lbMaChi.Text;
                cmd.Parameters.Add(new SqlParameter("ngaychi",SqlDbType.DateTime)).Value = dtNgayChi.Value;
                cmd.Parameters.Add(new SqlParameter("manv", SqlDbType.NVarChar)).Value = cobNguoiChi.SelectedValue;
                if (cmd.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Sửa thông tin chi tiết chi thành công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    this.Close();
                }
            }
        }

        private void dtNgayChi_ValueChanged(object sender, EventArgs e)
        {
            int ngay, thang, nam;
            int n_ngay, n_thang, n_nam;
            ngay = dtNgayChi.Value.Day;
            thang = dtNgayChi.Value.Month;
            nam = dtNgayChi.Value.Year;

            n_ngay = DateTime.Now.Day;
            n_thang = DateTime.Now.Month;
            n_nam = DateTime.Now.Year;
            if (ngay > n_ngay || thang > n_thang || nam > n_nam)
            {
                MessageBox.Show("Bạn chọn ngày lập phiếu chi sai!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtNgayChi.Focus();
                dtNgayChi.Value = new DateTime(n_nam, n_thang, n_ngay);
            }
        }

        private void btnInPhieu_Click(object sender, EventArgs e)
        {
            frmInPhieuChi _frminphieuchi = new frmInPhieuChi();
            _frminphieuchi.ShowDialog();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
